home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / VideoToolboxSources / GetScreenDevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-26  |  6.3 KB  |  192 lines  |  [TEXT/KAHL]

  1. /* GetScreenDevice.c
  2. PROBLEM:
  3. How can I tell whether I've got a CGrafPtr or a GWorldPtr? (In one case I'll call 
  4. GetGWorldDevice, in the other case I'll get max device.)
  5. I noticed that the portVersions are 0xc001 and 0xc000. Should I use that to tell?
  6.  
  7. ANSWER FROM APPLE DEVSUPPORT (8/93):
  8. You can use GetGWorldDevice() on GWorldPtr, GrafPort or CGrafPort. Use
  9. TestDeviceAttibute() to test if the returned GDHandle is a "real" screen or not.
  10.  
  11. Also, you should refer to the tech note “RowBytes Revealed II” (available on the
  12. June Developer CD) which states:
  13.  
  14. You'll notice in Figure 1 that the portVersion field of a cGrafPort coincides
  15. with the location of the rowBytes field of a grafPort. Remember, a cGrafPort has
  16. the same size as a grafPort. During debugging, you can use the same information
  17. that CopyBits uses to identify cGrafPorts.
  18.  
  19. If you use a grafPort template to display memory for an unknown grafPort, you
  20. can tell if it is a cGrafPort because the rowBytes will be equal to 0xC000. The
  21. 0xC corresponds to the two high bits being set in the portVersion field of a
  22. cGrafPort. Since these bits can not be set in a grafPort, you know you have a
  23. cGrafPort. In addition, if the bottom bit of the portVersion field is set, then
  24. it is a gWorld. Thus, if your rowBytes field has a value of 0xC001, then you
  25. know you have a gWorld.
  26.  
  27. CONCLUSION:
  28. GetWindowDevice() assumes that it's a GWorld iff the bits 0xC001 of the
  29. are set. From Apple's comment above this is clearly true at present and probably
  30. safe for the future.
  31.  
  32. Copyright © 1989-1993 Denis G. Pelli
  33. HISTORY:
  34. 3/20/90        dgp    make compatible with MPW C.
  35. 3/22/90    dgp    changed GetDeviceSlot to use the AuxDCEHandle instead of deducing it
  36.             from the baseAddr of the PixMap. This is a cleaner way to do it.
  37. 4/9/90    dgp    eliminated #define for Mainscrn mispelling in Color.h
  38. 10/17/90 dgp Added AddressToScreenDevice() for compatibility with built-in video on
  39.             the Mac IIci, IIsi, and LC.
  40. 10/18/90 dgp Added LocalToGlobalRect() and GetWindowDevice().
  41. 8/24/91        dgp    Made compatible with THINK C 5.0.
  42. 2/1/92    dgp    Fixed bugs in GetWindowDevice() which resulted in returning garbage GDHandle.
  43. 3/3/92    dgp    In GetScreenDevice(), skip inactive screens.
  44. 8/20/92    dgp    expanded comments of GetDeviceSlot(), noting that it works even with
  45.             built-in video, e.g. on Mac IIci.
  46. 8/26/92    dgp    GetDeviceSlot() now returns -1 if none, since zero is a legal slot.
  47.             GetScreenDevice() first checks for 8-bit QuickDraw().
  48. 9/10/92    dgp    Actually implemented the 8/26 change instead of just changing the 
  49.             documentation. Oops.
  50. 4/17/93    dgp Deleted obsolete AddressToSlot and AddressToScreenDevice.
  51. 5/21/93    dgp    Fixed GetWindowDevice() to support GWorld's.
  52. 8/14/93    dgp    Based on answer from DEVSUPPORT, I cleaned up GetWindowDevice().
  53. 4/11/94    dgp    Added GetRectDevice() based on code extracted from GetWindowDevice().
  54.  
  55. */
  56. #include "VideoToolbox.h"
  57. GDHandle GetRectDevice(Rect *r);
  58.  
  59. GDHandle GetScreenDevice(int n)
  60. // Returns a handle to the n-th screen, where the MainDevice is the zero-th screen.
  61. // Returns NULL if request can't be satisfied.
  62. {
  63.     GDHandle device;
  64.     int i,error;
  65.     long value;
  66.  
  67.     if(n<0)return NULL;
  68.     error=Gestalt(gestaltQuickdrawVersion,&value);
  69.     if(error || value<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  70.     if(n==0) return GetMainDevice();
  71.     device=GetDeviceList();
  72.     i=0;
  73.     while(device!=NULL){
  74.         if (TestDeviceAttribute(device,screenDevice)
  75.             && !TestDeviceAttribute(device,mainScreen)
  76.             && TestDeviceAttribute(device,screenActive)){
  77.                 i++;
  78.                 if(i==n)break;
  79.             }
  80.         device = GetNextDevice(device);
  81.     }
  82.     return device;
  83. }
  84.  
  85. int GetScreenIndex(GDHandle device)
  86. // Inverse of GetScreenDevice(). Returns -1 if request can't be satisfied.
  87. {
  88.     int i,error;
  89.     long value;
  90.  
  91.     error=Gestalt(gestaltQuickdrawVersion,&value);
  92.     if(error || value<gestalt8BitQD)return 0;
  93.     if(device==NULL)return -1;
  94.     for(i=0;i<16;i++)if(device==GetScreenDevice(i))return i;
  95.     return -1;
  96. }
  97.  
  98. short int GetDeviceSlot(GDHandle device)
  99. // Gets the "slot" for any screen device, even if it's built-in video, e.g. on Mac
  100. // IIci or Quadra. See 1992 Inside Mac "Processes" page 4-11. Returns -1 if none.
  101. // Zero is a legal slot for built-in video devices.
  102. {
  103.     AuxDCEHandle myAuxDCEHandle;
  104.  
  105.     if(device == NULL) return -1;
  106.     myAuxDCEHandle=(AuxDCEHandle) GetDCtlEntry((**device).gdRefNum);
  107.     return ((**myAuxDCEHandle).dCtlSlot);
  108. }
  109.  
  110. GDHandle SlotToScreenDevice(int n)
  111. // Returns a handle to the screen device in slot n.
  112. // Returns NULL if request can't be satisfied.
  113. {
  114.     GDHandle device;
  115.  
  116.     device=GetDeviceList();
  117.     while(device!=NULL) {
  118.         if (TestDeviceAttribute(device,screenDevice) &&
  119.             GetDeviceSlot(device)==n)
  120.                 break;
  121.         device=GetNextDevice(device);
  122.     }
  123.     return device;
  124. }
  125.  
  126. GDHandle GetWindowDevice(WindowPtr window)
  127. // For on-screen window, returns GDHandle of screen with largest intersection with the 
  128. // window's content.
  129. // For off-screen window (i.e. GWorld), it returns the GDHandle of the associated device.
  130. {
  131.     Rect r;
  132.     WindowPtr oldWindow;
  133.     long qD;
  134.  
  135.     if(window==NULL)return NULL;
  136.     Gestalt(gestaltQuickdrawVersion,&qD);
  137.     if(qD>=gestalt32BitQD && (((CWindowPtr)window)->portVersion&0xc001)==0xc001){
  138.         // It's a GWorld iff the portVersion has both high bits set (cGrafPort) and
  139.         // the low bit set (GWorld). See Apple Tech Note “RowBytes Revealed II”.
  140.         return GetGWorldDevice((GWorldPtr)window);
  141.     }
  142.     r=window->portRect;
  143.     GetPort(&oldWindow);
  144.     SetPort(window);
  145.     LocalToGlobalRect(&r);
  146.     SetPort(oldWindow);
  147.     return GetRectDevice(&r);
  148. }
  149.  
  150. GDHandle GetRectDevice(Rect *r)
  151. // Returns GDHandle of screen with largest intersection with the global rect.
  152. {
  153.     Rect overlap;
  154.     GDHandle device,dominantDevice=NULL;
  155.     long area,greatestArea;
  156.     long qD;
  157.  
  158.     if(r==NULL)return NULL;
  159.     Gestalt(gestaltQuickdrawVersion,&qD);
  160.     if(qD<gestalt8BitQD)return NULL;    // need 8-bit quickdraw
  161.     device=GetDeviceList();
  162.     greatestArea=0;
  163.     while(device!=NULL){
  164.         if(TestDeviceAttribute(device,screenDevice)
  165.             && TestDeviceAttribute(device,screenActive)){
  166.                 SectRect(r,&(*device)->gdRect,&overlap);
  167.                 area=(long)(overlap.right-overlap.left)*(overlap.bottom-overlap.top);
  168.                 if(area>greatestArea){
  169.                     greatestArea=area;
  170.                     dominantDevice=device;
  171.                 }
  172.             }
  173.         device=GetNextDevice(device);
  174.     }
  175.     return dominantDevice;
  176. }
  177.  
  178. void LocalToGlobalRect(Rect *r)
  179. {
  180.     Point pt={0,0};
  181.     
  182.     LocalToGlobal(&pt);
  183.     OffsetRect(r,pt.h,pt.v);
  184. }
  185.  
  186. void GlobalToLocalRect(Rect *r)
  187. {
  188.     Point pt={0,0};
  189.     
  190.     GlobalToLocal(&pt);
  191.     OffsetRect(r,pt.h,pt.v);
  192. }